import { Component, OnDestroy, OnInit } from '@angular/core';
import { UserFavorsProvider } from './user.favors.provider';
import { Subscription } from 'rxjs/Subscription';
import { Util } from '../../../util/util';
import { Router } from '@angular/router';
import { UserManhuaBase } from '../user.manhua.base';

@Component({
    selector: 'user-favors',
    templateUrl: './user.favors.html',
    styleUrls: ['./user.favors.css'],
    providers: [UserFavorsProvider]
})

export class UserFavors extends UserManhuaBase{


    favors: any = [];
    manhuaEditBool: boolean = false;
    page: any = {
        size: 12,
        curPage: 1,
        pages: 0,
        totalCount: 1
    };
    constructor(private service: UserFavorsProvider, public util: Util,public router: Router) { 
        super(util,router);
    }

    ngOnInit() {
        this.getManhuaFavors(this.page);
    }

     // page nav
     pageChange(cur) {

        this.page.curPage = cur;

        this.getManhuaFavors(this.page);

    }

    pageSet(_page) {
        this.page.pages = _page.pages;
        this.page.curPage = _page.curPage;
        this.page.totalCount = _page.totalCount;
    }

    manhuaEdit() {
        this.manhuaEditBool = !this.manhuaEditBool;
    }
    cancel() {
        for (let i in this.favors) {
            if (this.favors[i]['deleteBool'] === true) {
                this.favors[i]['deleteBool'] = false;
            }
        }

        this.manhuaEditBool = !this.manhuaEditBool;
    }

    comfirm() {
        for (let i in this.favors) {
            let item = this.favors[i];
            if (item['deleteBool'] === true) {
                this.unfavor(item);
            }
            item = null;
        }
    }

   

    // 删除用户收藏的漫画
    delAjaxSub: Subscription;
    unfavor(favor_manhua) {
        let json = {
            "_id": favor_manhua['_id']
        };

        this.delAjaxSub = this.service.unfavor(json).subscribe((res: any) => {
            if (res.success) {
                this.getManhuaFavors(this.page);
            }else{

            }

        }, () => {

        });
    }

    // 获取用户收藏的漫画
    ajaxSub: Subscription;
    getManhuaFavors(param) {
        this.ajaxSub = this.service.getManhuaFavors(param).subscribe((res: any) => {
            if (res.success) {
                this.pageSet(res.page);
                this.favors = res.data;

                for (let i in this.favors) {

                    let _item: any = this.favors[i];

                    let _sub = this.service.getManhuaDetail({ '_id': _item['manhuaId'] }).subscribe((res: any) => {

                        if (res.success && res.data.length > 0) {

                            let temp: boolean = false;
                            if (res.data[0]['chapters'] && (res.data[0]['chapters'].length > _item['chapterLangth'])) {
                                temp = true;
                            } else {
                                temp = false;
                            }

                            // 对比
                            if (_item['isNew'] !== temp) {

                                let uAjaxSub = this.service.update(_item).subscribe(() => {
                                    uAjaxSub.unsubscribe();
                                    uAjaxSub = null;
                                }, (err) => {
                                    uAjaxSub.unsubscribe();
                                    uAjaxSub = null;
                                });
                            }

                        } else {

                        }
                        _sub.unsubscribe();
                        _sub = null;


                    }, () => {
                        _sub.unsubscribe();
                        _sub = null;
                    });
                }



            } else {
                this.favors = [];
            }
        }, () => { });
    }

    ngOnDestroy() {
        if (this.ajaxSub) {
            this.ajaxSub.unsubscribe();
        }

        if (this.delAjaxSub) {
            this.delAjaxSub.unsubscribe();
        }
    }
}